const cloud = require('@alipay/faas-server-sdk');
/**
 * 获取订单编号
 */
function generateOrderNumber() {
    const timestamp = Date.now().toString(); // 获取当前时间戳
    const random = Math.floor(Math.random() * 1000).toString().padStart(3, '0'); // 生成3位随机数
    return `${timestamp}${random}`; // 组合时间戳和随机数
  }
exports.main = async (event, context) => {
  const db = cloud.database();
  const response = {};
  const orderNumber = generateOrderNumber();
  response.orderNumber = orderNumber;
  response.totalAmount = event.totalAmount;
  //新增订单
  const orderInfoResp = await db.collection('mall_order_info').add({
    data: {
      "userId": event.userId,
      "orderNo":orderNumber,
      "totalCount":event.totalCount,
      "totalAmount":event.totalAmount,
      "freight":event.freight,
      "payType":"ALIPAY",
      "addressInfo":event.addressInfo,
      "status":"未付款"
    }
  });
  console.log(`orderInfoResp:`, orderInfoResp);
  //新增订单明细
  const res = await cloud.callFunction({
    name: 'orderDetailAdd',
    data: {
      "orderId":orderInfoResp._id,
      "orderGoods":event.orderGoods
    }
  });
  response.id = orderInfoResp._id;
  return response;

};